package com.jeady.timetodo.activities.vibrate

import android.content.Context
import android.os.VibrationEffect
import android.os.VibratorManager
import android.util.Log
import kotlin.random.Random.Default.nextInt
import kotlin.random.Random.Default.nextLong

object VibrateController{
    private val TAG = "[VibrateController]"
    fun vibrate(context: Context){
        Log.d(TAG, "vibrate: ")
        val vibrateManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
        val vibrator = vibrateManager.defaultVibrator
        if(vibrator.hasVibrator()){
            vibrator.vibrate(
                VibrationEffect.createWaveform(
                    LongArray(100).map {
                        return@map nextLong(1000L, 5000L)
                    }.toLongArray(),
                    IntArray(100).map {
                        return@map nextInt(0, 255)
                    }.toIntArray(),
                    1
                )
            )
        }
    }
    fun stopVibrate(context: Context){
        Log.d(TAG, "stopVibrate: ")
        val vibrateManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
        val vibrator = vibrateManager.defaultVibrator
        if(vibrator.hasVibrator()){
            vibrator.cancel()
        }
    }
}